/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Bean;

import java.sql.SQLException;
import java.util.Objects;
import db.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author 1Stmax
 */
public class buildingtype {

    private int buildingtypeid;
    private String buildingtypename;
    private String buildingroomname;

    public buildingtype() {
    }

    public int getBuildingtypeid() {
        return buildingtypeid;
    }

    public void setBuildingtypeid(int buildingtypeid) {
        this.buildingtypeid = buildingtypeid;
    }

    public String getBuildingtypename() {
        return buildingtypename;
    }

    public void setBuildingtypename(String buildingtypename) {
        this.buildingtypename = buildingtypename;
    }

    public String getBuildingroomname() {
        return buildingroomname;
    }

    public void setBuildingroomname(String buildingroomname) {
        this.buildingroomname = buildingroomname;
    }

    public ArrayList<buildingtype> getAllBuildingType() {

        Connection con = null;
        ResultSet rs = null;
        Statement stmt = null;
        ArrayList<buildingtype> a=new ArrayList();
        try {
            con = getDBConnection.getConnection();
            stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
            String sql = "SELECT * FROM BuildingType";
            System.out.println("Executing:" + sql);
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                buildingtype b = new buildingtype();
                b.setBuildingtypeid(new Integer(rs.getInt("BuildingTypeID")));
                b.setBuildingtypename(rs.getString("BuildingTypeName"));
                b.setBuildingroomname(rs.getString("BuildingRoomName"));
                a.add(b);
            }
        } catch (SQLException ex) {
            Logger.getLogger(buildingtype.class.getName()).log(Level.SEVERE, null, ex);
        }
        return a;
    }

    public buildingtype findBuildingtypeByID(int id) {
        buildingtype selectBuildingtype = new buildingtype();
        Connection con = null;
        ResultSet rs = null;
        Statement stmt = null;
        try {
            con = getDBConnection.getConnection();
            stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
            String query = "SELECT * FROM BuildingType WHERE BuildingTypeID=" + id;
            System.out.println("\nExecutiing:" + query);
            rs = stmt.executeQuery(query);
            while (rs.next()) {
                selectBuildingtype.setBuildingtypeid(new Integer(rs.getInt("BuildingTypeID")));
                selectBuildingtype.setBuildingtypename(rs.getString("BuildingTypeName"));
                selectBuildingtype.setBuildingroomname(rs.getString("BuildingRoomName"));
            }

        } catch (SQLException ex) {
            Logger.getLogger(buildingtype.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                con.close();
            } catch (SQLException ex) {
                Logger.getLogger(buildingtype.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return selectBuildingtype;
    }

    public String updateBuildingtype(int buildingtypeid, String buildingtypename, String buildingroomname) {
        buildingtype oldBuildingtype = findBuildingtypeByID(buildingtypeid);
        Connection conn = null;
        ResultSet rs = null;
        Statement stmt = null;
        try {
            conn = getDBConnection.getConnection();
            stmt = conn.createStatement(rs.TYPE_SCROLL_SENSITIVE, rs.CONCUR_READ_ONLY);
            StringBuffer columns = new StringBuffer(225);
            if (buildingtypename != null
                    && !buildingtypename.equals(oldBuildingtype.getBuildingtypename())) {
                columns.append("buildingtypename = '" + buildingtypename + "'");
            }
            if (buildingroomname != null
                    && !buildingroomname.equals(oldBuildingtype.getBuildingroomname())) {
                if (columns.length() > 0) {
                    columns.append(", ");
                }
                columns.append("buildingroomname = '" + buildingroomname + "'");
            }
            //When the whole set of changes has been assembled, check to see whether there are in fact any changes,
            //that is, whether the StringBuffer contains anything. If so,
            //construct a SQL UPDATE statement using the information in the StringBuffer and execute it.
            //If the StringBuffer does not contain any changes, output a message saying so:
            if (columns.length() > 0) {
                String sqlString = "update BuildingType SET " + columns.toString()
                        + " WHERE buildingtypeid = " + buildingtypeid;
                System.out.println("\nExecuting: " + sqlString);
                stmt.execute(sqlString);
            } else {
                System.out.println("Nothing to do to update buidingtype Id: "
                        + buildingtypeid);
            }
        } //end update buiding type
        catch (SQLException ex) {
            Logger.getLogger(buildingtype.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "success";
    }//end update buiding type

    public String deleteBuildingtype(int id) {
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            con = getDBConnection.getConnection();
            stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            String sql = "DELETE";
        } catch (SQLException ex) {
            Logger.getLogger(buildingtype.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                con.close();
            } catch (SQLException ex) {
                Logger.getLogger(buildingtype.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return "success";
    }

    public String insertBuildingtype(String buildingtypename, String buildingroomname) {
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            con = getDBConnection.getConnection();
            stmt = con.createStatement();
            String sql="INSERT INTO BuildingType values("+buildingtypename+","+buildingroomname+");";
            rs=stmt.executeQuery(sql);
            while(rs.next()){
                System.out.println("Exectig...:"+sql);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
            Logger.getLogger(buildingtype.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                con.close();
            } catch (SQLException ex) {
                Logger.getLogger(buildingtype.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return "success";
    }
}
